package com.laikan.reader.core.view;

import android.animation.ObjectAnimator;
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;

/**
 * Created by wx
 *
 * @date 2016/4/13/16:05
 * @description 下拉图片会变大
 * android:scaleType="centerCrop" 设置图片类型
 */
public class ImageViewPullScroll extends ScrollView {

    private LinearLayout contentView;//scroll唯一子布局
    private RelativeLayout relativeLayoutTile;// 图片布局
    private ImageView img;//图片
    private int imageHeight = 0;//图片的高度
    private OnScrollListener onScrollListener;//滚动接口
    //获取屏幕的高
    private boolean isLayout = true; //单次获取图片的的高度
    private int screenHeight;//屏幕的高
    private int screenWidth;//屏幕的宽
    private RelativeLayout toumingceng;

    public ImageViewPullScroll(Context context) {
        super(context);
        screenHeight = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getHeight();
        screenWidth = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getWidth();
    }

    public ImageViewPullScroll(Context context, AttributeSet attrs) {
        super(context, attrs);
        screenHeight = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getHeight();
        screenWidth = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getWidth();
    }

    /**
     * 滚动的回调接口
     *
     * @author wx
     */
    public interface OnScrollListener {

        /**
         * 回调方法， 返回MyScrollView滑动的Y方向距离
         *
         * @param scrollY 、
         */
        void onScroll(int scrollY);
    }

    public void setOnScrollListener(OnScrollListener l) {
        this.onScrollListener = l;
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        if(this.getChildCount() > 0) {
            contentView = (LinearLayout) getChildAt(0);
            relativeLayoutTile = (RelativeLayout)contentView.getChildAt(0);
            img = (ImageView) relativeLayoutTile.getChildAt(0);
        }
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
        if(isLayout) {
            isLayout = false;
            imageHeight = img.getHeight();

        }
    }

    int imageAddHeight;

    @Override
    protected boolean overScrollBy(int deltaX, int deltaY, int scrollX, int scrollY, int scrollRangeX, int scrollRangeY, int maxOverScrollX, int maxOverScrollY, boolean isTouchEvent) {

        if(!isContent()) {
            return super.overScrollBy(deltaX, deltaY, scrollX, scrollY, scrollRangeX, scrollRangeY, maxOverScrollX, maxOverScrollY, isTouchEvent);
        }
        if(!isCanPullDown()) {
            return super.overScrollBy(deltaX, deltaY, scrollX, scrollY, scrollRangeX, scrollRangeY, maxOverScrollX, maxOverScrollY, isTouchEvent);
        }
        if(isTouchEvent && scrollY <= 0 && deltaY < 0) {
            imageAddHeight = (int) (img.getHeight() + Math.abs(deltaY) / 3.0f);
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams((int) (screenWidth + Math.abs(deltaY) / 2.0f),
                imageAddHeight);
            img.setLayoutParams(params);
            //            imageView.getLayoutParams().height = imageAddHeight;
            img.requestLayout();
        }else if(isTouchEvent && deltaY > 0 && scrollY == 0) {
            imageAddHeight = (int) (img.getHeight() - Math.abs(deltaY) / 2.0f);
            if(imageAddHeight >= imageHeight) {
                //                imageView.getLayoutParams().height = imageAddHeight;
                //                imageView.requestLayout();

                RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams((int) (screenWidth + Math.abs(deltaY) / 2.0f),
                    imageAddHeight);
                img.setLayoutParams(params);
                //            imageView.getLayoutParams().height = imageAddHeight;
                img.requestLayout();
                return false;
            }

        }

        return super.overScrollBy(deltaX, deltaY, scrollX, scrollY, scrollRangeX, scrollRangeY, maxOverScrollX, maxOverScrollY, isTouchEvent);

    }

    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        if(onScrollListener != null) {
            onScrollListener.onScroll(t);
        }
        super.onScrollChanged(l, t, oldl, oldt);
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        switch(ev.getAction()) {
            case MotionEvent.ACTION_UP:

                ViewWrapper wrapper = new ViewWrapper(img);
                ObjectAnimator.ofInt(wrapper, "height", imageHeight).setDuration(500).start();
                break;
        }
        return super.onTouchEvent(ev);
    }

    private static class ViewWrapper {

        private View mTarget;

        public ViewWrapper(View target) {
            mTarget = target;
        }

        public int getHeight() {
            return mTarget.getLayoutParams().height;
        }

        public void setHeight(int height) {
            mTarget.getLayoutParams().height = height;
            mTarget.requestLayout();
        }
    }

    /**
     * 判断当前子view是否充满
     *
     * @return
     */
    private boolean isContent() {
        return contentView.getHeight() > screenHeight;
    }

    /**
     * 判断是否滚动到顶部
     */
    private boolean isCanPullDown() {
        return isContent() && (getScrollY() == 0 || contentView.getHeight() < getHeight() + getScrollY());
    }

    /**
     * 判断是否滚动到底部
     */
    private boolean isCanPullUp() {
        return isContent() && (contentView.getHeight() <= getHeight() + getScrollY());
    }

}
